package com.msun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msun.dao.RoleDao;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.pojo.Menu;
import com.msun.pojo.Permission;
import com.msun.pojo.Role;
import com.msun.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
        //select * from t_checkitem limit 0,10
        Page<Role> page = roleDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total,rows);

    }

    @Override
    public Role findById(Integer id) {
        Role role= roleDao.findById(id);
        return role;
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {

        roleDao.addRole(role);

        Integer RoleId = role.getId();

      /*  Long count= roleDao.findCountFoodById(RoleId);
        Long count1= roleDao.findCountSportsById(RoleId);

        if(count >0){
            roleDao.deleteFoodById(OrderId);
        }

        if(count1 >0){
            roleDao.deleteSportsById(OrderId);
        }*/

        if(permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("RoleId",role.getId());
                map.put("permissionId",permissionId);
                roleDao.insertRoleIdAndpermission(map);
            }
        }
        if(menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("RoleId",role.getId());
                map.put("menuId",menuId);
                roleDao.insertRoleIdAndMenu(map);
            }
        }
    }

    @Override
    public List<Permission> findAllPermissions() {
        List<Permission> list= roleDao.findAllPermissions();
        return list;
    }

    @Override
    public List<Menu> findAllMenus() {
        List<Menu> list= roleDao.findAllMenus();
        return list;
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        List<Integer> list= roleDao.findMenuIdsByRoleId(id);
        return list;
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        List<Integer> list= roleDao.findPermissionIdsByRoleId(id);
        return list;
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {

        Integer RoleId = role.getId();

        List<Integer> menuIdsByRoleId = roleDao.findMenuIdsByRoleId(RoleId);
        List<Integer> permissionIdsByRoleId = roleDao.findPermissionIdsByRoleId(RoleId);

        if(menuIdsByRoleId != null){
            roleDao.deleteMenuById(RoleId);
        }

        if(permissionIdsByRoleId != null){
            roleDao.deletePermissionById(RoleId);
        }

        if(permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("RoleId",role.getId());
                map.put("permissionId",permissionId);
                roleDao.insertRoleIdAndpermission(map);
            }
        }
        if(menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("RoleId",role.getId());
                map.put("menuId",menuId);
                roleDao.insertRoleIdAndMenu(map);
            }
        }
        roleDao.edit(role);
    }
    @Override
    public List<Role> findAllCheckGroupByUserId(Integer userId) {

        return roleDao.findAllCheckGroupByUserId(userId);
    }
    @Override
    public void delete(Integer id) {


        List<Integer> menuIdsByRoleId = roleDao.findMenuIdsByRoleId(id);
        List<Integer> permissionIdsByRoleId = roleDao.findPermissionIdsByRoleId(id);

        if(menuIdsByRoleId != null){
            roleDao.deleteMenuById(id);
        }

        if(permissionIdsByRoleId != null){
            roleDao.deletePermissionById(id);
        }

        roleDao.delete(id);
    }
}
